package com.singfan.core;

import com.singfan.common.SingFanException;
import com.singfan.common.db.DbContext;
import com.singfan.common.util.StringUtil;
import com.singfan.core.util.SmsUtil;

import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;

/**
 * Created by hansonghe on 15/8/13.
 */
public class Test {
    public static void main(String[] args){
        System.out.println(new Date(1446091489962L - 10 * 60000));
        System.out.println(UUID.randomUUID().toString().substring(0,8));

    }

    public static void printDaoImplInfo(){
        DbContext.init();

        Connection connection = DbContext.getConnectionPoolProvider().getConnection("app_1");
        try {
            String catalog = connection.getCatalog();
            DatabaseMetaData dbMetaData = connection.getMetaData();
            ResultSet tablesResultSet = dbMetaData.getTables(catalog, null, null, new String[]{"TABLE"});
            while(tablesResultSet.next()){
                String tableName = tablesResultSet.getString("TABLE_NAME");
                String className = StringUtil.commonName2JavaClassName(tableName);
                String daoImplTemp = "package com.singfan.core.dao.impl;\n" +
                        "\n" +
                        "import com.singfan.common.db.DaoBaseGeneric;\n" +
                        "import com.singfan.common.db.DbContext;\n" +
                        "import com.singfan.common.db.config.Table;\n" +
                        "import com.singfan.core.dao.I#ModelName#Dao;\n" +
                        "import com.singfan.core.dao.database.model.#ModelName#;\n" +
                        "import org.springframework.stereotype.Repository;\n" +
                        "\n" +
                        "/**\n" +
                        " *\n" +
                        " * Author: hansonghe\n" +
                        " * Date: 2015-08-19 12:12\n" +
                        " * Desc:\n" +
                        " */\n" +
                        "@Repository\n" +
                        "public class #ModelName#DaoImpl extends DaoBaseGeneric<#ModelName#> implements I#ModelName#Dao {\n" +
                        "    @Override\n" +
                        "    protected String getTableName(Object hashKey) {\n" +
                        "        return tableNameBase;\n" +
                        "    }\n" +
                        "\n" +
                        "    @Override\n" +
                        "    protected String getDatabaseId(Object hashKey) {\n" +
                        "        Table table = DbContext.getDbConfigProvider().getTable(tableNameBase);\n" +
                        "        return table.getDatabaseIdList(table.getNewestVersion()).get(0);\n" +
                        "    }\n" +
                        "}\n";
                String daoImplInfo = daoImplTemp.replace("#ModelName#", className);
                createFile(className + "DaoImpl.java", daoImplInfo);
                //System.out.println(daoImplInfo);
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void printDaoInfo(){
        DbContext.init();

        Connection connection = DbContext.getConnectionPoolProvider().getConnection("app_1");
        try {
            String catalog = connection.getCatalog();
            DatabaseMetaData dbMetaData = connection.getMetaData();
            ResultSet tablesResultSet = dbMetaData.getTables(catalog, null, null, new String[]{"TABLE"});
            while(tablesResultSet.next()){
                String tableName = tablesResultSet.getString("TABLE_NAME");
                String className = StringUtil.commonName2JavaClassName(tableName);
                String idaoInfo = String.format("package com.singfan.core.dao;\n" +
                        "\n" +
                        "/**\n" +
                        " *\n" +
                        " * Author: hansonghe\n" +
                        " * Date: 2015-08-19 12:12\n" +
                        " * Desc:\n" +
                        " */\n" +
                        "public interface I%1$sDao {\n" +
                        "}", className);
                createFile("I" + className + "Dao.java", idaoInfo);
                //System.out.println(idaoInfo);
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void printDbModelInfo(){
        DbContext.init();

        Connection connection = DbContext.getConnectionPoolProvider().getConnection("app_1");
        try {
            String catalog = connection.getCatalog();
            DatabaseMetaData dbMetaData = connection.getMetaData();
            ResultSet tablesResultSet = dbMetaData.getTables(catalog, null, null, new String[]{"TABLE"});
            while(tablesResultSet.next()){
                String tableName = tablesResultSet.getString("TABLE_NAME");
                System.out.println("package com.singfan.core.model;\n" +
                        "\n" +
                        "import com.singfan.common.db.annotation.Primarykey;\n" +
                        "\n" +
                        "/**\n" +
                        " * \n" +
                        " * Author: hansonghe\n" +
                        " * Date: 2015-08-19 12:12\n" +
                        " * Desc:\n" +
                        " */");
                System.out.println(String.format("public class %1$s {", StringUtil.commonName2JavaClassName(tableName)));

                ResultSet colRet = dbMetaData.getColumns(catalog, "%", tableName, "%");
                while(colRet.next()) {
                    String columnName = colRet.getString("COLUMN_NAME");
                    String columnType = colRet.getString("TYPE_NAME");
                    String javaType;
                    if (columnType.equalsIgnoreCase("INT")){
                        javaType = "int";
                    }
                    else if (columnType.equalsIgnoreCase("TINYINT")){
                        javaType = "int";
                    }
                    else if (columnType.equalsIgnoreCase("BIGINT")){
                        javaType = "long";
                    }
                    else if (columnType.equalsIgnoreCase("DECIMAL")){
                        javaType = "double";
                    }
                    else if (columnType.equalsIgnoreCase("VARCHAR")){
                        javaType = "String";
                    }
                    else if (columnType.equalsIgnoreCase("TEXT")){
                        javaType = "String";
                    }
                    else if (columnType.equalsIgnoreCase("BIT")){
                        javaType = "boolean";
                    }
                    else if (columnType.equalsIgnoreCase("DATETIME")){
                        javaType = "Date";
                    }
                    else {
                        javaType = columnType;
                    }
                    String columnRemarks = colRet.getString("REMARKS");
                    if (StringUtil.isNullOrEmpty(columnRemarks) || columnName.equals("id")){
                        columnRemarks = "主键";
                    }
                    System.out.println("    //" + columnRemarks);
                    if (columnName.equals("id")){
                        System.out.println("    @Primarykey");
                    }
                    System.out.println(String.format("    private %1$s %2$s;", javaType, StringUtil.commonName2JavaFieldName(columnName)));
                }

                System.out.println("}");
                System.out.println();
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void createFile(String fileName, String fileContent){
        File file = new File(fileName);
        try {
            file.createNewFile();
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            writer.write(fileContent);
            writer.close();
        }
        catch (Exception ex){
            throw new SingFanException(String.format("FileUtil.readLineText(file:%s) error: %s !", file.getName(), ex.getMessage()), ex);
        }
    }
}
